/******************************************************************************
 * Copyright (C) 2011 ShenZhen ComTop Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳康拓普开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 *****************************************************************************/

package com.code.common.excel.officeexport.excel.help;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.code.common.excel.officeexport.excel.model.ExportFieldVO;

/**
 * 
 * 格式基类
 *
 * @author  李鹏
 * @since   1.0
 * @version 2013-3-20 李鹏
 */
public class ExportCellFormat {

    /** 模板路径 */
    final private String path;

    /** 默认单元格格式 */
    private HSSFCellStyle cellStyle = null;

    /** 单元格格式存储 */
    private final Map<String, HSSFCellStyle> format = new HashMap<String, HSSFCellStyle>();

    /** 默认字体 */
    private HSSFFont font = null;

    /**
     * 构造函数
     */
    public ExportCellFormat() {
        this.path = null;
    }

    /**
     * 构造函数
     *
     * @param path 模板路径
     */
    public ExportCellFormat(String path) {
        this.path =System.getProperty("webRoot.root") + ExcelExportConstants.EXCEL_EXPORT_PATH + path;
    }

    /**
     * 设置标题样式
     *
     * @param workbook 工作表
     * @param exportField 单元格
     * @return 单元格样式
     * @throws OfficeException 如果在运行过程当中出现错误就抛出异常OfficeException.
     */
    public HSSFCellStyle getTitleStyle(HSSFWorkbook workbook, ExportFieldVO exportField) throws Exception {
        HSSFCellStyle objHSSFCellStyle = exportField.getStyle();
        if (objHSSFCellStyle == null) {
            objHSSFCellStyle = workbook.createCellStyle();
            HSSFFont objFont = setDefaultTitleFont(workbook);
            objFont.setColor(exportField.getColor());
            objHSSFCellStyle.setFont(objFont);
            this.setAlign(exportField, objHSSFCellStyle);
            setDefaultBorder(objHSSFCellStyle);
        }
        return objHSSFCellStyle;
    }

    /**
     * 设置头样式
     *
     * @param workbook 工作表
     * @param exportField 单元格
     * @return 单元格样式
     * @throws OfficeException 如果在运行过程当中出现错误就抛出异常OfficeException.
     */
    public HSSFCellStyle getHeadStyle(HSSFWorkbook workbook, ExportFieldVO exportField) throws Exception {
        HSSFCellStyle objHSSFCellStyle = exportField.getStyle();
        if (objHSSFCellStyle == null) {
            objHSSFCellStyle = workbook.createCellStyle();
            HSSFFont objFont = setDefaultHeadFont(workbook);
            objFont.setColor(exportField.getColor());
            objHSSFCellStyle.setFont(objFont);
            setAlign(exportField, objHSSFCellStyle);
            objHSSFCellStyle.setWrapText(true);
            objHSSFCellStyle.setFillBackgroundColor(HSSFColor.DARK_BLUE.index);
            objHSSFCellStyle.setFillPattern(HSSFCellStyle.BIG_SPOTS);
            setDefaultBorder(objHSSFCellStyle);
        }
        return objHSSFCellStyle;
    }

    /**
     * 默认body样式
     *
     * @param workbook 工作表
     * @param exportField 单元格
     * @return 单元格样式
     */
    public HSSFCellStyle getBodyStyle(HSSFWorkbook workbook, ExportFieldVO exportField) {
        HSSFCellStyle objHSSFCellStyle = exportField.getStyle();
        if (objHSSFCellStyle == null) {
            if (cellStyle == null) {
                cellStyle = workbook.createCellStyle();
                font = workbook.createFont();
                font.setColor(exportField.getColor());
                cellStyle.setFont(font);
            }
            objHSSFCellStyle = cellStyle;
        }
        return objHSSFCellStyle;
    }

    /**
     * 单元格样式
     *
     * @param workbook 工作表
     * @param exportField 单元格
     * @param sheetName 工作表
     * @param column 单元格
     * @return 单元格样式
     * @throws Exception 
     */
    public HSSFCellStyle getModelBodyStyle(HSSFWorkbook workbook, ExportFieldVO exportField, String sheetName,
        int column) throws Exception {
        HSSFCellStyle objHSSFCellStyle = exportField.getStyle();
        if (objHSSFCellStyle == null) {
            String strKey = column + "#" + sheetName;
            if (format.containsKey(strKey)) {
                return format.get(strKey);
            }
            FileInputStream objFileIn = null;
            try {
                objFileIn = new FileInputStream(path);
                POIFSFileSystem objPOIFile = new POIFSFileSystem(objFileIn);
                HSSFWorkbook objWorkbook = new HSSFWorkbook(objPOIFile);
                HSSFSheet objSheet = objWorkbook.getSheet(sheetName);
                int iStartRow = objSheet.getLastRowNum();
                HSSFRow objRow = objSheet.getRow(iStartRow);
                HSSFCell objCell = objRow.getCell(column);
                HSSFCellStyle objCellStyle = objCell.getCellStyle();
                objHSSFCellStyle = workbook.createCellStyle();
                if (objCellStyle != null) {
                    setCellStyle(workbook, objWorkbook, objCellStyle, objHSSFCellStyle);
                }
                format.put(strKey, objHSSFCellStyle);
                return objHSSFCellStyle;
            } catch (FileNotFoundException e) {
                throw new Exception("读取模板文件时发生错误", e);
            } catch (IOException e) {
                throw new Exception("读取模板文件时发生错误", e);
            } finally {
                if (objFileIn != null) {
                    objFileIn.close();
                }
            }
        }
        return objHSSFCellStyle;
    }

    /**
     * 设置单元格样式
     *
     * @param workbook 目的工作表
     * @param objWorkbook 源工作表
     * @param objCellStyle 源单元格样式
     * @param objHSSFCellStyle 目的单元格样式
     */
    private void setCellStyle(HSSFWorkbook workbook, HSSFWorkbook objWorkbook, HSSFCellStyle objCellStyle,
        HSSFCellStyle objHSSFCellStyle) {
        objHSSFCellStyle.setAlignment(objCellStyle.getAlignment());
        objHSSFCellStyle.setFillPattern(objCellStyle.getFillPattern());
        objHSSFCellStyle.setFillBackgroundColor(objCellStyle.getFillBackgroundColor());
        objHSSFCellStyle.setFillForegroundColor(objCellStyle.getFillForegroundColor());
        objHSSFCellStyle.setBorderBottom(objCellStyle.getBorderBottom());
        objHSSFCellStyle.setBorderLeft(objCellStyle.getBorderLeft());
        objHSSFCellStyle.setBorderRight(objCellStyle.getBorderRight());
        objHSSFCellStyle.setBorderTop(objCellStyle.getBorderTop());
        objHSSFCellStyle.setBottomBorderColor(objCellStyle.getBottomBorderColor());
        objHSSFCellStyle.setDataFormat(objCellStyle.getDataFormat());
        HSSFFont objHSSFFont = setFont(workbook, objWorkbook, objCellStyle);
        objHSSFCellStyle.setFont(objHSSFFont);
        objHSSFCellStyle.setHidden(objCellStyle.getHidden());
        objHSSFCellStyle.setIndention(objCellStyle.getIndention());
        objHSSFCellStyle.setLeftBorderColor(objCellStyle.getLeftBorderColor());
        objHSSFCellStyle.setLocked(objCellStyle.getLocked());
        objHSSFCellStyle.setRightBorderColor(objCellStyle.getRightBorderColor());
        objHSSFCellStyle.setRotation(objCellStyle.getRotation());
        objHSSFCellStyle.setTopBorderColor(objCellStyle.getTopBorderColor());
        objHSSFCellStyle.setVerticalAlignment(objCellStyle.getVerticalAlignment());
        objHSSFCellStyle.setWrapText(objCellStyle.getWrapText());
    }

    /**
     * 设置字体
     *
     * @param workbook 目的工作表
     * @param objWorkbook 源工作表
     * @param objCellStyle 源单元格样式
     * @return 字体
     */
    private HSSFFont setFont(HSSFWorkbook workbook, HSSFWorkbook objWorkbook, HSSFCellStyle objCellStyle) {
        HSSFFont objHSSFFont = workbook.createFont();
        HSSFFont objFont = objWorkbook.getFontAt(objCellStyle.getFontIndex());
        objHSSFFont.setBoldweight(objFont.getBoldweight());
        objHSSFFont.setColor(objFont.getColor());
        objHSSFFont.setFontHeight(objFont.getFontHeight());
        objHSSFFont.setFontHeightInPoints(objFont.getFontHeightInPoints());
        objHSSFFont.setFontName(objFont.getFontName());
        objHSSFFont.setItalic(objFont.getItalic());
        objHSSFFont.setStrikeout(objFont.getStrikeout());
        objHSSFFont.setTypeOffset(objFont.getTypeOffset());
        objHSSFFont.setUnderline(objFont.getUnderline());
        return objHSSFFont;
    }

    /**
     * 设置默认边框格式
     *
     * @param objHSSFCellStyle 样式
     */
    public void setDefaultBorder(HSSFCellStyle objHSSFCellStyle) {
        objHSSFCellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
        objHSSFCellStyle.setTopBorderColor(HSSFColor.BLACK.index);
        objHSSFCellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
        objHSSFCellStyle.setRightBorderColor(HSSFColor.BLACK.index);
        objHSSFCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        objHSSFCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        objHSSFCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        objHSSFCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
    }

    /**
     * 设置标题默认字体
     *
     * @param workbook 工作表
     * @return 字体
     */
    private HSSFFont setDefaultTitleFont(HSSFWorkbook workbook) {
        HSSFFont objFont = workbook.createFont();
        objFont.setFontName("宋体");
        objFont.setFontHeight((short) 500);
        objFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        return objFont;
    }

    /**
     * 设置居中
     *
     * @param exportField 单元格
     * @param objHSSFCellStyle 样式
     */
    public void setAlign(ExportFieldVO exportField, HSSFCellStyle objHSSFCellStyle) {
        if ("CENTRE".equalsIgnoreCase(exportField.getAlign())) {
            objHSSFCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        } else if ("LEFT".equalsIgnoreCase(exportField.getAlign())) {
            objHSSFCellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        } else if ("RIGHT".equalsIgnoreCase(exportField.getAlign())) {
            objHSSFCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        }
        objHSSFCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    }

    /**
     * 设置题头默认字体
     *
     * @param workbook 工作表
     * @return 字体
     */
    private HSSFFont setDefaultHeadFont(HSSFWorkbook workbook) {
        HSSFFont objFont = workbook.createFont();
        objFont.setFontName("宋体");
        objFont.setFontHeight((short) 200);
        objFont.setColor(HSSFColor.WHITE.index);
        objFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        objFont.setItalic(true);
        return objFont;
    }

    /**
     * 清空格式
     */
    public void clearFormat() {
        this.format.clear();
    }
}
